package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.model.TbSeckillGoods;
import com.dongdongshop.model.TbSeckillGoodsExample;
import com.dongdongshop.model.TbSeckillOrder;
import com.dongdongshop.service.SeckillService;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.vo.SeckillGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SeckillServiceImpl implements SeckillService {
    @Resource
    private TbSeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Resource
    private TbSeckillOrderMapper seckillOrderMapper;
    @Override
    public void catchPreHot() {
        //定义一个当前时间
        Date now = new Date();
        TbSeckillGoodsExample seckillGoodsExample = new TbSeckillGoodsExample();
        TbSeckillGoodsExample.Criteria criteria = seckillGoodsExample.createCriteria();
        criteria.andStartTimeLessThanOrEqualTo(now);//开始时间小于等于当前时间
        criteria.andEndTimeGreaterThanOrEqualTo(now);//结束时间大于等于当前时间
        List<TbSeckillGoods> tbSeckillGoods = seckillGoodsMapper.selectByExample(seckillGoodsExample);
        //转vo
        tbSeckillGoods.stream().map(s -> {
            SeckillGoodsVO goodsVO = new SeckillGoodsVO();
            BeanUtils.copyProperties(s, goodsVO);
            return goodsVO;
        }).forEach(v -> {
            //存入redis
            redisTemplate.boundHashOps("seckill").put(v.getId(),v);
        });
    }

    @Override
    public List<SeckillGoodsVO> seckillList() {
        List<SeckillGoodsVO> seckill = (List<SeckillGoodsVO>)redisTemplate.boundHashOps("seckill").values();
        return seckill;
    }

    @Override
    //@SentinelResource("seckillGoods")
    public SeckillGoodsVO seckillGoods(Long id) {
        return (SeckillGoodsVO)redisTemplate.boundHashOps("seckill").get(id);
    }

    @Override
    public void submitOrder(String userId, Long id) {
        //创建订单
        //先根据商品id查询,从redis中取出要秒杀的商品
        SeckillGoodsVO goodsVO = seckillGoods(id);
        if (goodsVO == null){
            throw new RuntimeException("秒杀的商品不存在");
        }
        //判断时间是否有效
        if (( goodsVO.getEndTime().getTime() -new Date().getTime() ) <= 0){
            throw new RuntimeException("秒杀已经结束");
        }
        //判断库存是否还有
        if(goodsVO.getStockCount() <= 0){
            throw new RuntimeException("商品已经售罄");
        }
        //扣减库存
        goodsVO.setStockCount(goodsVO.getStockCount() - 1);
        //同步到redis中
        redisTemplate.boundHashOps("seckill").put(goodsVO.getId(),goodsVO);
        //当减到最后一个商品,同步到数据库
        if(goodsVO.getStockCount() == 0){
            TbSeckillGoods seckillGoods = new TbSeckillGoods();
            BeanUtils.copyProperties(goodsVO,seckillGoods);
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
        }
        //生成订单,创建
        TbSeckillOrder seckillOrder = new TbSeckillOrder();
        seckillOrder.setCreateTime(new Date());//创建时间
        seckillOrder.setId(idWorker.nextId());//订单id
        seckillOrder.setMoney(goodsVO.getCostPrice());//秒杀价格
        seckillOrder.setSellerId(goodsVO.getSellerId());//商家id
        seckillOrder.setStatus("0");//待支付
        seckillOrder.setUserId(userId);//用户id
        seckillOrder.setSeckillId(goodsVO.getId());//秒杀商品id
        seckillOrderMapper.insertSelective(seckillOrder);
    }
}
